package com.zmc.common.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author：zmc
 * @Date：2024/11/13 14:50
 */

@Component
@PropertySource(value = "classpath:application-common.yml", factory = YamlPropertiesSourceFactory.class)
@ConfigurationProperties(prefix = "shop.thread")
@Data
public class ThreadPoolConfigProperties {

    private Integer core;

    private Integer queueCapacity;

    private Integer maxSize;

    private Integer keepAliveTime;

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
        return new ThreadPoolExecutor(core,
                maxSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(queueCapacity),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
